package com.gitee.feizns.explore.data_structure.array.easy;

import java.util.Arrays;

/**
 * @author feizns
 * @since 2020/1/8
 */
public class Merge {

    public static void main(String[] args) {
        int[] tmp = new int[]{ 4, 0, 0, 0, 0, 0};
        merge(tmp, 1, new int[]{1, 2, 3, 4, 5}, 5);
        System.out.println(Arrays.toString(tmp));
    }

    public static void merge(int[] num1, int m, int[] num2, int n) {
        int pIndex = num1.length - m;
        for (int i = m - 1; i >= 0; i--)
            num1[i + pIndex] = num1[i];
        int index = 0;
        int nIndex = 0;
        while ( index < num1.length ) {
            if ( pIndex == num1.length )
                num1[index++] = num2[nIndex++];
            else if ( nIndex == num2.length )
                num1[index++] = num1[pIndex++];
            else if ( num1[pIndex] < num2[nIndex] )
                num1[index++] = num1[pIndex++];
            else
                num1[index++] = num2[nIndex++];
        }
    }

}
